package com.yupfeg.test.share

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.yupfeg.livedata.LiveDataState
import com.yupfeg.livedata.R
import com.yupfeg.livedata.StateLiveData
import com.yupfeg.test.base.activityViewModelDelegate

/**
 * 作为事件接收方的Fragment
 * @author yuPFeG
 * @date 2021/09/19
 */
class ObserverFragment : Fragment(){

    private lateinit var tvEventContent : TextView
    private lateinit var tvStateContent : TextView
    private lateinit var tvStateName : TextView

    private val mActivityViewModel : FragmentSharedViewModel by activityViewModelDelegate()

    private var mSingleEventNum = 0
    private var mStateEventNum = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_observer,container,false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        tvEventContent = view.findViewById(R.id.tv_observer_single_event_content)
        tvStateContent = view.findViewById(R.id.tv_observer_single_state_content)
        tvStateName = view.findViewById(R.id.tv_observer_state_name)

        mActivityViewModel.singleEvent.observe(this.viewLifecycleOwner){eventContent->
            mSingleEventNum++
            tvEventContent.text = "接收到单次事件：${eventContent}${mSingleEventNum}"
        }

        mActivityViewModel.stateEvent.observeProcessState(this.viewLifecycleOwner,object :
            StateLiveData.ProcessedStatusObserver<String>{
            /**
             * LiveData状态变化时回调
             * */
            override fun onProcessedStatusChange(state: LiveDataState) {
                when(state){
                    is LiveDataState.Error -> {
                        tvStateName.text = "error"
                        Log.d("logger","observe LiveDataState Error")
                    }
                    is LiveDataState.Processed -> {
                        tvStateName.text = "Processed"
                        Log.d("logger","observe LiveDataState Processed")
                    }
                    is LiveDataState.Start -> {
                        tvStateName.text = "Start Loading"
                        Log.d("logger","observe LiveDataState Processed")
                    }
                    is LiveDataState.WaitProcess -> {
                        tvStateName.text = "WaitProcess"
                        Log.d("logger","observe LiveDataState WaitProcess")
                    }
                }
            }

            /**
             * Called when the data is changed.
             * @param t  The new data
             */
            override fun onChanged(t: String?) {
                mStateEventNum++
                tvStateContent.text = "接收到StateLiveData事件：${t}${mStateEventNum}"
            }

        })

    }


}